/*
 * 新增合作伙伴
 * @Author: ljb 
 * @Date: 2017-11-27 15:23:58 
 * @Last Modified by: ljb
 * @Last Modified time: 2018-01-27 16:04:25
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>

    .v-admin-partner-create {
        min-height: 600px;
    }
    
</style>
<template>
<div class="v-admin-partner-create">
    <Row type="flex" justify="start">
        <Col push="1">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">

                <Form-item label="名称" prop="name">
                    <Input v-model="formValidate.name" placeholder="请输入名称"></Input>
                </Form-item>

                <!-- <Form-item label="官网地址" prop="url">
                    <Input v-model="formValidate.url" type="url" placeholder="请输入官网地址"></Input>
                </Form-item> -->

                <Form-item label="优先级" prop="priority">
                    <InputNumber :max="100" :min="1" v-model="formValidate.priority" placeholder="请输入优先级"></InputNumber>
                </Form-item>

                <Form-item label="logo" prop="logo">
                    <Upload :before-upload="file=>fileChange(file,'logo','image_info')"
                            :on-remove="file=>fileChange(file,'logo','image_info')"
                            :multiple="false"
                            accept="image/jpg, image/jpeg, image/png"
                            action="//fake_upload_url/">
                        <Button type="ghost" icon="ios-cloud-upload-outline">上传图片</Button>
                        <div class="license-tip" slot="tip" v-if="image_info.name">{{ image_info.name }}</div>
                        <!-- <div class="license-tip" slot="tip" v-else>图片规格为320 x 134的jpg、jpeg或png，大小不超1.5M</div> -->
                    </Upload>
                </Form-item>

                <Form-item label="备注" prop="remark">
                    <Input v-model="formValidate.remark" type="textarea" :autosize="{minRows: 4,maxRows: 5}" placeholder="请输入备注"></Input>
                </Form-item>
                
                <Form-item>
                    <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                    <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
                </Form-item>

            </Form>
        </Col>
    </Row>
</div>
</template>

<script>

import { mapGetters } from 'vuex';

import {
    validateImage
} from 'src/validate';    //引入校验文件

export default {
    name: 'partner-create',
    data() {	
        return {

            formValidate : {

                name : '',

                // url : '',

                logo : '',

                priority : 1,

                remark : ''

            },                  //新增合作伙伴时的表单

            ruleValidate: {
                name: [
                    { required: true, message: '名称不能为空', trigger: 'blur,change' }
                ],
                // url: [
                //     { required: true, message: '网址不能为空', trigger: 'blur,change' },
                //     { type: 'url', message: '网址格式不对', trigger: 'blur,change' },
                // ],
                logo: [
                    { required: true, validator: validateImage, trigger: 'blur,change' }
                ],
                priority: [
                    { required: true, type: 'number', message: '优先级不能为空', trigger: 'change' },
                ],
                // remark: [
                //     { required: true, message: '备注不能为空', trigger: 'blur,change' },
                // ]
            },

            // 上传的图片信息
            image_info : {
                name: '',
                url: '这个地方可以 base64 编码的图片，然后给 modal 框去显示图片'
            }
        }
    },
    methods: {

        /**
         * file类型input改变
         * @param  {Object} file {选择的文件对象}
         * @return {Boolean} {false则会停止上传}
         */
        fileChange(file,param,info){

            this[info].name = file.name;

            this.formValidate[param] = file;

            return false;

        },

        /**
         * 提交表单
         * @param  {string} name {表单的ref}
         * @return {undefined}
         */
        handleSubmit (name) {
            this.$refs[name].validate((valid) => {
                if (valid) {

                    this.$emit('post',this.formDataCreate());

                } else {

                    this.$Message.error('存在未填写的条目!');

                }
            })
        },

        /**
         * 重置表单
         * @param  {string} name {表单的ref}
         * @return {undefined} 
         */
        handleReset (name) {

            this.$refs[name].resetFields();

            this.formValidate.logo = '';

            this.image_info.name = '';

        },

        /**
         * 手动拼接formdata数据
         * @return {Object} {拼接好的formdata数据}
         */
        formDataCreate(){

            const new_object = {

                name : this.formValidate.name,

                // url : this.formValidate.url,

                logo : this.formValidate.logo,

                priority : this.formValidate.priority,

                remark : this.formValidate.remark

            };

            let form_data = new FormData();

            Object.keys(new_object).forEach((key)=>{

                form_data.append( key , new_object[key] );
        
            });

            return form_data;

        }
        
    }
}
</script>